//
//  AIChargeListController.m
//  HNBaseProject
//
//  Created by xujie on 2024/7/19.
//

#import "AIChargeListController.h"
#import "AIChargeListCell.h"
#import "AIChargeListModel.h"
@interface AIChargeListController () <MxNavigationViewDelegate,UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIView *nodataV;
@property (nonatomic, strong) MxPublicNav *nav;
@property (nonatomic, strong) UITableView *listTab;
@property (nonatomic, strong) NSMutableArray *listArray;
@end

@implementation AIChargeListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F8FB"];
    self.listArray = [[NSMutableArray alloc] init];
    [self createUI];
}

- (void)createUI {
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.backgroundColor = [UIColor whiteColor];
    self.nav.titleLab.text = self.fromCost ? [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"消费", nil),NSLocalizedString(@"记录", nil)]: [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"充值", nil),NSLocalizedString(@"记录", nil)];
    self.nav.delegate = self;
    [self.view addSubview:self.nav];
    
    [self.view addSubview:self.listTab];
    [self.listTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.mas_equalTo(self.nav.mas_bottom);
    }];
    
    [self.view addSubview:self.nodataV];
    self.nodataV.hidden = YES;
    [self.nodataV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.mas_equalTo(80);
        make.center.mas_equalTo(self.view);
    }];
    [self getList];
}

- (void)getList {
    NSDictionary *param = @{@"type":self.type,
                            @"pay_status" : self.pay_status
    };
    MJWeakSelf
    [[DCHttpClient shareClient] requestWithPath:@"api/recharge.UserRechargeLog/lists" params:param httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        [weakSelf.view hideToastActivity];
        if([dict[@"code"] intValue]==1){
            NSArray *dataArray =  [NSArray modelArrayWithClass:[AIChargeListModel class] json:dict[@"data"][@"lists"]];
            [weakSelf.listArray addObjectsFromArray:dataArray];
            [weakSelf.listTab reloadData];
            weakSelf.nodataV.hidden = weakSelf.listArray.count >0;
        } else {
            weakSelf.nodataV.hidden = NO;
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        weakSelf.nodataV.hidden = NO;
        [weakSelf.view hideToastActivity];
    }];
}

#pragma mark - ________ UITableView ________

- (UITableView *)listTab {
    
    if (nil == _listTab) {
        _listTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listTab.backgroundColor = [UIColor clearColor];
        _listTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTab.delegate = self;
        _listTab.dataSource = self;
        _listTab.rowHeight = 90.0;
        _listTab.showsVerticalScrollIndicator = NO;
        _listTab.showsHorizontalScrollIndicator= NO;
        [_listTab registerClass:[AIChargeListCell class] forCellReuseIdentifier:NSStringFromClass([AIChargeListCell class])];
        if (@available(iOS 11.0, *)) {
            _listTab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _listTab.sectionHeaderTopPadding = 0;
        }
    }
    return _listTab;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AIChargeListModel *model = self.listArray[indexPath.row];
    if (self.fromCost) {
        model.remark = @"AI制作";
    }
    AIChargeListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AIChargeListCell class])];
    [cell reloadCellWithModel:model];
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIView *)nodataV {
    if (!_nodataV) {
        _nodataV = [[UIView alloc] init];
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"history_nodata"]];
        [_nodataV addSubview:icon];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_nodataV);
            make.top.offset(0);
        }];
        
        UILabel *lab = [[UILabel alloc] init];
        lab.text = @"暂无信息";
        lab.font = PFRFont(12);
        lab.textColor = [UIColor colorWithHexString:@"#B7B7B7"];
        [_nodataV addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_nodataV);
            make.top.mas_equalTo(icon.mas_bottom).offset(11);
        }];
    }
    return _nodataV;
}

@end
